import modelExtend from 'dva-model-extend';
import queryString from 'query-string';
import { create, remove, update, list, get } from './../services/users';
import { pageModel } from './common';

export default modelExtend(pageModel, {
    namespace: 'user',
    state: {
        currentItem: {},
        modalVisible: false,
        modalType: 'create',
    },

    subscriptions: {
        setup ({ dispatch, history }) {
            history.listen((location) => {
                if (location.pathname === '/user') {
                    const payload = queryString.parse(location.search) || { page: 1, pageSize: 10 };
                    dispatch({
                        type: 'query',
                        payload,
                    })
                }
            })
        },
    },

    effects: {
        * query ({ payload = {} }, { call, put }) {
            const data = yield call(list, payload);
            if (data.success) {
                yield put({
                    type: 'querySuccess',
                    payload: {
                        list: data.data,
                        pagination: {
                            current: Number(payload.page) || 1,
                            pageSize: Number(payload.pageSize) || 10,
                            total: data.total,
                        },
                    },
                })
            } else {
                throw data
            }
        },

        * delete ({ payload }, { call, put }) {
            const data = yield call(remove, payload);
            if (data.success) {
                yield put({ type: 'query' })
            } else {
                throw data
            }
        },

        * create ({ payload }, { call, put }) {
            const data = yield call(create, payload);
            if (data.success) {
                yield put({ type: 'hideModal' });
                yield put({ type: 'query' })
            } else {
                throw data
            }
        },

        * update ({ payload }, { select, call, put }) {
            const id = yield select(({ user }) => user.currentItem.id);
            const newUser = { ...payload, id };
            const data = yield call(update, newUser);
            if (data.success) {
                yield put({ type: 'hideModal' });
                yield put({ type: 'query' })
            } else {
                throw data
            }
        },

        * get ({ payload }, { call, put }){
            const data = yield call(get, payload);
            if (data.success){
                yield put({
                    type: 'showModal',
                    payload:{
                        modalType: 'update',
                        currentItem: data.data
                    }
                });
            } else {
                throw data
            }
        }
    },

    reducers: {
        showModal (state, { payload }) {
            return { ...state, ...payload, modalVisible: true }
        },
        hideModal (state) {
            return { ...state, modalVisible: false }
        }
    },
})
